/**
 * @author https://vue-admin-beautiful.com （不想保留author可删除）
 * @description 路由守卫，目前两种模式：all模式与intelligence模式
 */
import router from '@/router'
import store from '@/store'
import VabProgress from 'nprogress'
import 'nprogress/nprogress.css'
import getPageTitle from '@/utils/pageTitle'
import {
  authentication,
  loginInterception,
  progressBar,
  recordRoute,
  routesWhiteList,
} from '@/config'

VabProgress.configure({
  easing: 'ease',
  speed: 500,
  trickleSpeed: 200,
  showSpinner: false,
})
router.beforeResolve(async (to, from, next) => {
  if (progressBar) VabProgress.start()
  let hasToken = store.getters['user/accessToken']
  if (hasToken) {
    let sign = store.getters.sign
    if (sign) {
      next()
    } else {
      const { menus } = await store.dispatch('baseStore/getUserInfo')
      await store.dispatch('routes/setRoutes', menus)
      next()
      // accessRoutes.forEach((item) => {
      //   router.addRoute(item)
      // })
      // next({ ...to, replace: true })
    }
  } else {
    VabProgress.done()
    if (routesWhiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
  document.title = getPageTitle(to.meta.title)
})
router.afterEach(() => {
  if (progressBar) VabProgress.done()
})
